package by.grsu.bso.beans;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import by.grsu.bso.model.Product;
import by.grsu.bso.service.ServiceManager;
import by.grsu.bso.util.ValueHelper;

public class ProductUtilBean {

	public ProductUtilBean() {

	}

	public List<Product> getProducts(String parCatId) {
		int parentCategoryId = 0;
		try {
			parentCategoryId = Integer.parseInt(parCatId);
		} catch (Exception e) {
		}

		List<Product> productList = new ArrayList<Product>();
		try {
			productList = ServiceManager.getProductService()
					.getProductsByCategory(parentCategoryId);
		} catch (SQLException e) {
			e.printStackTrace();
		}

		return productList;

	}
	
	public List<Product> getAllProducts(){
		List<Product> productList = new ArrayList<Product>();
		try {
			productList =  ServiceManager.getProductService().findAll();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return productList;
	}

	public Product getProduct(String testId) {
		int productId = ValueHelper.getIntParameter(testId, -1);
		
		return getProduct(productId);
	}
	
	public Product getProduct(int productId) {
		
		Product product = null;
		try {
			product = ServiceManager.getProductService().findById(productId);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return product;
	}

}
